package com.eshop.controller.user;

import com.eshop.pojo.RespBean;
import com.eshop.pojo.User;
import com.eshop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spi.service.contexts.SecurityContext;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 个人中心
 * @author guyongrui
 */
@Api(tags = "个人中心管理相关")
@RestController
@RequestMapping("/userinfo")
public class UserInfoController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/")
    public RespBean updateUser(@RequestBody User user, Authentication authentication){
        if (userService.updateById(user)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(user,
                    null,authentication.getAuthorities()));
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "更新当前用户密码")
    @PutMapping("/password")
    public RespBean updateUserPassword(@RequestBody Map<String,Object> info) {
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer userId = (Integer) info.get("userId");
        return userService.updateUserPassword(oldPass,pass,userId);
    }

    @ApiOperation(value = "更新用户头像")
    @PostMapping("/userface")
    public RespBean updateUserFace(@PathVariable MultipartFile file,Integer id,Authentication authentication) throws IOException {
        if(file.isEmpty()){
            return RespBean.error("请选择文件!");
        }
        String fileName = file.getOriginalFilename();//文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
        String path = "D:\\咫尺易市\\前端\\public\\img"; //文件存储位置
        fileName= UUID.randomUUID()+suffixName;//图片名
        File dest = new File(path+"/"+fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
        String url="/img/"+fileName;
            User user = (User) authentication.getPrincipal();
            user.setUserFace(url);
            userService.updateById(user);
//            System.out.println(user.getUsername());
            //更新全局Authentication
            SecurityContextHolder.getContext().setAuthentication
                    (new UsernamePasswordAuthenticationToken(user, authentication.getCredentials(), authentication.getAuthorities()));
        return userService.updateUserFace(url,id,authentication);
    }

}
